package com.dream.service.user.web.processor.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.dream.common.model.bean.SerVersionBean;
import com.dream.common.model.response.BaseListResponse;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.result.AppResultBuilder;
import com.dream.common.toolkits.clickstream.ClickStreamClient;
import com.dream.common.toolkits.clickstream.bean.ClickStreamBaseRequest;
import com.dream.common.toolkits.clickstream.bean.ExtradataBase;
import com.dream.common.toolkits.clickstream.bean.LogType;
import com.dream.common.toolkits.clickstream.bean.SkipType;
import com.dream.service.user.api.UserFollowService;
import com.dream.service.user.domain.entity.IsFollowCollect;
import com.dream.service.user.web.model.FieldSchema;
import com.dream.service.user.web.processor.RequestProcessor;
import com.dream.service.user.web.util.UserWebUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

import static com.dream.service.user.web.model.Param.CMD;
import static com.dream.service.user.web.model.Param.UserFollowCMD;

/**
 * Created by houlijiang on 2016/5/18.
 * <p>
 * 设计师关注相关对外接口
 */
public class UserFollowRequestProcessor implements RequestProcessor {

    UserFollowService userFollowService;

    public UserFollowService getUserFollowService() {
        return userFollowService;
    }

    public void setUserFollowService(UserFollowService userFollowService) {
        this.userFollowService = userFollowService;
    }

    @Override
    public JSONObject processor(HttpServletRequest req) {
        JSONObject result = new JSONObject();
        String cmd = req.getParameter(CMD);
        try {
            if (cmd.equals(UserFollowCMD.USER_FOLLOW_LIST)) {
                this.getFollowList(req, result);
            } else if (cmd.equals(UserFollowCMD.USER_ADD_FOLLOW)) {
                this.addFollow(req, result);
            } else if (cmd.equals(UserFollowCMD.USER_DEL_FOLLOW)) {
                this.delFollow(req, result);
            } else if (cmd.equals(UserFollowCMD.USER_IS_FOLLOW)) {
                this.isFollow(req, result);
            }else {
                AppResultBuilder.buildCmdErrorResult(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildResult(result, "", false, ErrorCode.UNKOWN);
        }
        return result;
    }


    //增加 follow
    //http://localhost:8080/follow?cmd=add&pin=44df39b02fc14055abf97d0aadf0e34f&did=69&token=111&src=iOS&version=1.0.1
    private void addFollow(HttpServletRequest req, JSONObject result) {
        String pin = req.getParameter(FieldSchema.PIN);
        String idStr = req.getParameter(FieldSchema.DESIGNER_ID);
        if (StringUtils.isEmpty(pin) || StringUtils.isEmpty(idStr)) {
            AppResultBuilder.buildParamErrorResult(result, "pin or id is null", "");
            return;
        }
        long designerId;
        try {
            designerId = Long.parseLong(idStr);
        } catch (Exception e) {
            AppResultBuilder.buildParamErrorResult(result, "id is invalid", "");
            return;
        }
        BaseResponse<Boolean> response;
        try {
            response = userFollowService.addFollow(pin, Arrays.asList(designerId));
            if (response.isSuccess()) {
                AppResultBuilder.buildSucResult(result, new Object());
            } else {
                AppResultBuilder.buildErrorResult(result, response.getError_code(), response.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }finally {
            try {
                //加入wishlist埋点
                SerVersionBean serVersionBean= UserWebUtil.buildSerVersionBean(req);
                ClickStreamClient clickStreamClient = new ClickStreamClient();
                ExtradataBase extradataBase = new ExtradataBase(SkipType.DESIGNER,String.valueOf(designerId));
                ClickStreamBaseRequest clickStreamBaseRequest = new ClickStreamBaseRequest(LogType.FOLLOW_ADD,
                        req.getParameter(FieldSchema.UUID),pin,serVersionBean.getSrc(),
                        serVersionBean.getVer(),serVersionBean.getVersion());
                clickStreamClient.reportClickStream(clickStreamBaseRequest,extradataBase);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //删除 follow
    private void delFollow(HttpServletRequest req, JSONObject result) {
        String pin = req.getParameter(FieldSchema.PIN);
        String idStr = req.getParameter(FieldSchema.DESIGNER_ID);
        if (StringUtils.isEmpty(pin) || StringUtils.isEmpty(idStr)) {
            AppResultBuilder.buildParamErrorResult(result, "pin or id is null", "");
            return;
        }
        long designerId;
        try {
            designerId = Long.parseLong(idStr);
        } catch (Exception e) {
            AppResultBuilder.buildParamErrorResult(result, "id is invalid", "");
            return;
        }
        BaseResponse<Boolean> response;
        try {
            response = userFollowService.delFollow(pin, Arrays.asList(designerId));
            if (response.isSuccess()) {
                AppResultBuilder.buildSucResult(result, new Object());
            } else {
                AppResultBuilder.buildErrorResult(result, response.getError_code(), response.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }finally {
            try {
                //加入wishlist埋点
                SerVersionBean serVersionBean= UserWebUtil.buildSerVersionBean(req);
                ClickStreamClient clickStreamClient = new ClickStreamClient();
                ExtradataBase extradataBase = new ExtradataBase(SkipType.DESIGNER,String.valueOf(designerId));
                ClickStreamBaseRequest clickStreamBaseRequest = new ClickStreamBaseRequest(LogType.FOLLOW_DEL,
                        req.getParameter(FieldSchema.UUID),pin,serVersionBean.getSrc(),
                        serVersionBean.getVer(),serVersionBean.getVersion());
                clickStreamClient.reportClickStream(clickStreamBaseRequest,extradataBase);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    //拉取 folow list
    private void getFollowList(HttpServletRequest req, JSONObject result) {
        String pin = req.getParameter(FieldSchema.PIN);
        if (StringUtils.isEmpty(pin)) {
            AppResultBuilder.buildParamErrorResult(result, "pin null", "");
            return;
        }

        //分页
        Integer pageNum=null;
        Integer pageSize=null;
        String pageNumString = req.getParameter(FieldSchema.PAGE_NUM);
        String pageSizeString = req.getParameter(FieldSchema.PAGE_SIZE);
        if(pageNumString!=null && pageSizeString != null){
            pageNum = Integer.valueOf(pageNumString);
            pageSize = Integer.valueOf(pageSizeString);
            if(pageNum<=0 || pageSize<0){
                AppResultBuilder.buildParamErrorResult(result,"pageNum or PageSize Erro","");
                return;
            }
        }
        BaseListResponse baseListResponse;
        try {
            baseListResponse = userFollowService.getFollowList(pin, pageNum, pageSize);
            if (baseListResponse.isSuccess()) {
                AppResultBuilder.buildSucResult(result, baseListResponse.getResult());
            } else {
                AppResultBuilder.buildErrorResult(result, baseListResponse.getError_code(), baseListResponse.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }

    }


    //验证是否是 follow
    private void isFollow(HttpServletRequest req, JSONObject result) {
        String pin = req.getParameter(FieldSchema.PIN);
        String idStr = req.getParameter(FieldSchema.DESIGNER_ID);
        if (StringUtils.isEmpty(pin) || StringUtils.isEmpty(idStr)) {
            AppResultBuilder.buildParamErrorResult(result, "pin or id is null", "");
            return;
        }
        long designerId;
        try {
            designerId = Long.parseLong(idStr);
        } catch (Exception e) {
            AppResultBuilder.buildParamErrorResult(result, "id is invalid", "");
            return;
        }
        BaseResponse<IsFollowCollect> response;
        try {
            response = userFollowService.isFollow(pin, designerId);
            if (response.isSuccess()) {
                AppResultBuilder.buildSucResult(result, response.getResult());
            } else {
                AppResultBuilder.buildErrorResult(result, response.getError_code(), response.getError_msg(), "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "Service Error");
        }
    }

}
